﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Data;

using System.Data.SqlClient;
using System.Configuration;
using BOMS.DBUtility;



namespace BOMS.DATA
{
    public class DataUti
    {
        /// <summary>
        /// 获取所有的设备Model（包括有效和无效的）
        /// </summary>
        /// <returns></returns>
        public List<EquipModel> GetEquipModelList()
        {
            DbHelperSQL.connectionString = ConfigurationManager.ConnectionStrings["SQLConnString"].ConnectionString;
            string sql = "select ID,ClientHostIP,Port,Mac,Memo,Isvalid from dbo.W_Equips";
            DataTable dt = DbHelperSQL.Query(sql).Tables[0];
            List<EquipModel> list = new List<EquipModel>();
            EquipModel model = null;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                model = new EquipModel();
                model.ClientHostIP = dt.Rows[i]["ClientHostIP"].ToString();
                model.Port = dt.Rows[i]["Port"]==DBNull.Value?0:(int)dt.Rows[i]["Port"];
                model.Mac = dt.Rows[i]["Mac"] == DBNull.Value ? "" : dt.Rows[i]["Mac"].ToString(); 
                model.Memo = dt.Rows[i]["Memo"] == DBNull.Value ? "" : dt.Rows[i]["Memo"].ToString();
                model.IsValid = (bool)dt.Rows[i]["IsValid"];
                list.Add(model);
            }
            return list;
        }
        /// <summary>
        /// 获取所有有效的设备Model
        /// </summary>
        /// <param name="IsValid"></param>
        /// <returns></returns>
        public List<EquipModel> GetEquipModelList(bool IsValid)
        {           
            List<EquipModel> list = GetEquipModelList();
            List<EquipModel> validlist = list.FindAll(delegate(EquipModel model) { return model.IsValid = IsValid; });
            return validlist;
        }
        public void SaveClientIP(List<EquipModel> List)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" delete from dbo.W_Equips;");
            for (int i = 0; i < List.Count; i++)
            {
                sb.Append(" insert into dbo.W_Equips(ClientHostIp,Port) values('" + List[i].ClientHostIP + "','" + List[i].Port.ToString() + "');");
            }
            DbHelperSQL.ExecuteSql(sb.ToString());
        }
        public static string ImportEquipData(string ip, string mac, DateTime dtm, string xml)
        {

            DbHelperSQL.connectionString = ConfigurationManager.ConnectionStrings["SQLConnString"].ConnectionString;
            //DbHelperSQL.ExecuteSql("exec sp_sub_LoadLogData '" + EquipMac + "','" + dtm.ToString() + "','" + xmlData + "'");
            SqlParameter[] parms = new SqlParameter[4];
            (parms[0] = new SqlParameter("@Mac", SqlDbType.NVarChar, 40)).Value = mac;
            (parms[1] = new SqlParameter("@IP", SqlDbType.NVarChar, 40)).Value = ip;
            (parms[2] = new SqlParameter("@dtm", SqlDbType.DateTime)).Value = dtm;
            (parms[3] = new SqlParameter("@xmldata", SqlDbType.NVarChar, -1)).Value = xml;
            SqlDataReader dr = DbHelperSQL.RunProcedure("sp_sub_LoadLogData", parms);
            dr.Dispose();
            dr.Close();
            return "OK";
        }

    }
}
